package com.nhjf.service.impl.student;

import com.nhjf.command.student.CreateStudentCommand;
import com.nhjf.dao.student.StudentDao;
import com.nhjf.dao.system.UserDao;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.Role;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.Personalinformation.PersonalinformationQo;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.impl.BaseServiceImpl;
import com.nhjf.service.student.StudentService;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("studentService")
public class StudentServiceImpl extends BaseServiceImpl<Student,StudentQo,StudentDao> implements StudentService {
    @Autowired
    private StudentDao dao;
    @Autowired
    private UserDao userDao;
    @Override
    protected StudentDao getDao() {
        return dao;
    }
	@Override
	public ServiceResultDto createStudent(CreateStudentCommand createStudentrCommand) {
		// TODO Auto-generated method stub
		User user=new User();
		user.setUserName(createStudentrCommand.getUserName());
		user.setUserPassword(createStudentrCommand.getUserPassword());
		user.setItsusableornot(1);
		user.setIsRole(2);
		Role role=new Role();
		role.setId(4L);
		Set<Role> srole=new LinkedHashSet<Role>();
		srole.add(role);
		user.setRoleList(srole);
		userDao.save(user);
		Student student=new Student();
		student.setUser(user);
		student.setPhonenumber(createStudentrCommand.getUserName());
		student.setRegistrationtime(new Date());
		
		student.setNickname(createStudentrCommand.getUserName().replaceAll("^(.{3}).{4}(.+)$","$1****$2"));
		dao.save(student);
		ServiceResultDto serviceResultDto=new ServiceResultDto<>();
		serviceResultDto.setSuccess(true);
		serviceResultDto.setResultCode("200");
		serviceResultDto.setResultMessage("学生新增成功");
		return serviceResultDto;
	}
	@Override
	public ServiceResultDto Updateinformation(PersonalinformationQo qo) {
		// TODO Auto-generated method stub
		 ServiceResultDto resultDto = new ServiceResultDto();
		 UserQo userQo=new UserQo();
		 userQo.setSeachRole(true);
		 StudentQo studentQo=new StudentQo();
		 studentQo.setSeachStudent(true);
		 studentQo.setUserid(qo.getUserId());
		 Student student=dao.queryUnique(studentQo);
		  if(qo==null){
	             resultDto.setSuccess(false);
	             resultDto.setResultMessage("非法操作");
	             return resultDto;
	         }
		  userQo.setUserId(qo.getUserId());
		  User user=userDao.queryUnique(userQo);
		  //保存到User表个人介绍
		  user.setSelfintroduction(qo.getSelfintroduction());
		  //省 市 县
		  user.setProvince(qo.getAddressprovince());
		  user.setCity(qo.getAddresscity());
		  user.setCounty(qo.getCounty());
		  //头像
		  if(qo.getAvatar()!=null) {
			  student.setAvatar(qo.getAvatar());
		  }
		  //昵称
		  if(qo.getNickname()!=null) {
			  student.setNickname(qo.getNickname());
		  }else {
			  resultDto.setResultMessage("昵称不能为空！");
	             resultDto.setSuccess(false);
	             return resultDto;
		  }
		  //性别
		  if(qo.getGender()==1||qo.getGender()==2) {
			  student.setGender(qo.getGender()); 
		  }
		  if(qo.getIdentity()!=null) {
			  student.setIdentity(qo.getIdentity().toString());
		  }
		

		  userDao.update(user);
		  dao.update(student);
		  resultDto.setSuccess(true);
	         return resultDto;
		
	}
}
